package org.argeproje.resim.proc.tools;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class LoggerUT {
	private final static int LOG_ENTRY = 1;
	private final static int LOG_EXIT = 2;
	private final static int LOG_ERROR = 3;
	private final static int LOG_WARNING = 4;
	private final static int LOG_INFO = 5;
    private final static String fileName = "log.txt";
    //DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
    private static FileOutputStream logFile;
    public static void init(){
    	try {
			logFile = new FileOutputStream(fileName);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    public static void logEntry() {
		log(LOG_ENTRY, "");
	}

	public static void logExit() {
		log(LOG_EXIT, "");
	}

	public static void logError(String s) {
		log(LOG_ERROR, s);
	}

	public static void logWarning(String s) {
		log(LOG_WARNING, s);
	}

	public static void logInfo(String s) {
		log(LOG_INFO, s);
	}
    private static String getLogTypeAsString(int logType){
    	String s = "";
		switch (logType) {
		case LOG_ENTRY:
            s = "ENTRY  ";
			break;
		case LOG_EXIT:
			s = "EXIT   ";
			break;
		case LOG_ERROR:
			s = "ERROR  ";
			break;
		case LOG_WARNING:
			s = "WARNING";
			break;
		case LOG_INFO:
			s = "INFO   ";
			break;
		default:
			break;
		}
		return s;
    }
	private static void log(int logType, String s) {
		String logTypeStr = getLogTypeAsString(logType);
		String logMessage = new String("");
        StackTraceElement[] trace = new Throwable().getStackTrace();
        if (trace.length >= 2) {
            StackTraceElement elt = trace[2];
            logMessage = logTypeStr + " " + elt.getFileName() + " " + elt.getClassName() + "." +
                elt.getMethodName() + " line " + elt.getLineNumber() + "\n";
        } else
            System.out.println("[UNKNOWN CALLER]: " + s);
        try {
			logFile.write(logMessage.getBytes());
			logFile.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
